package com.trnnn.strutslit.filter;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import sun.net.www.MimeEntry;
import sun.net.www.MimeTable;

public class StaticResourceFilter {

	private static List<String> staticExt = new ArrayList<String>();
	static {
		Enumeration<MimeEntry> mimes = MimeTable.getDefaultTable().elements();
		while (mimes.hasMoreElements()) {
			MimeEntry entry = mimes.nextElement();
			String[] exts = entry.getExtensions();
			if (exts != null)
				for (int i = 0; i < exts.length; i++) {
					staticExt.add(exts[i]);
					System.out.println(exts[i]);
				}
		}
		staticExt.add(".jsp");
		staticExt.add(".js");
		staticExt.add(".css");
		System.out.println(staticExt.size());
	}

	public static boolean isStaticResource(String url) {
		String ext=getExtensionName(url);
		if(staticExt.contains(ext)){
			return true;
		}
		return false;
	}

	private static String getExtensionName(String url) {
		if ((url != null) && (url.length() > 0)) {
			int dot = url.lastIndexOf('.');
			if ((dot > -1) && (dot < (url.length() - 1))) {
				return url.substring(dot);
			}
		}
		return url;
	}

	public static void main(String[] args) {
		// Enumeration<MimeEntry> mimes=MimeTable.getDefaultTable().elements();
		// while(mimes.hasMoreElements()){
		// MimeEntry entry=mimes.nextElement();
		// String description=entry.getDescription();
		// String strs=entry.getExtensionsAsList();
		// String imageFilename=entry.getImageFileName();
		// String launchString=entry.getLaunchString();
		// String tempFileTemplete=entry.getTempFileTemplate();
		// String type=entry.getType();
		// System.out.println("description:"+description);
		// System.out.println("strs:"+strs);
		// System.out.println("imageFilename:"+imageFilename);
		// System.out.println("launchString:"+launchString);
		// System.out.println("tempFileTemplete:"+tempFileTemplete);
		// System.out.println("type:"+type);
		//
		// System.out.println("-------------------");
		// }
		String str = getExtensionName("http://ee.nedu.edu.cn/html/xkzy.html");
		System.out.println(staticExt.contains(".css")+"--"+str);
	}
}
